﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AntUnion.FrameWork.Model.BGModels;
using AntUnion.FrameWork.Model.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace AntUnion.FrameWork.Web.Areas.Console.Controllers
{
    [Area("Console")]
    public class HomeController : BaseController
    {
        private readonly mtContext _context;
        private readonly IWebHostEnvironment hostingEnv;
        public HomeController(mtContext context, IWebHostEnvironment env) : base(context, env)
        {
            _context = context;
            hostingEnv = env;
        }
        public IActionResult Index(string language = "manage-zh-CN")
        {
            string defaultLanguage = "zh-CN";
            if (language == "manage-en-US")
            {
                defaultLanguage = "en-US";
            }

            AdminModel model = GetSession<AdminModel>("User");
            TbUserRole userRole = _context.TbUserRoles.Where(k => k.UserId == model.Id).FirstOrDefault();
            TbRole role = _context.TbRoles.Where(k => k.Id == userRole.RoleId && k.UserType == 0).FirstOrDefault();
            List<Menu> menus = new List<Menu>();
            List<TbMenu> tbMenus = _context.TbMenus.Where(k => k.State == 1 && k.UserType == 0).ToList();
            if (model.Id == 1)
            {
                menus = tbMenus.Where(k => k.Pid == 0 && k.UserType == 0).OrderBy(k => k.Sort).Select(y => new Menu
                {
                    Id = y.Id,
                    Names = y.Names,
                    Url = y.Url,
                    Image = y.Image,
                    Remark = y.Remark,
                    children = tbMenus.Where(k => k.Pid == y.Id && k.UserType == 0).OrderBy(k => k.Sort).Select(x => new MenuTo
                    {
                        Id = x.Id,
                        Names = x.Names,
                        Url = x.Url,
                        Image = x.Image,
                        Remark = x.Remark,
                        msgCount = GetUnauditedCount(x.Remark),
                        children = tbMenus.Where(k => k.Pid == x.Id && k.UserType == 0).OrderBy(k => k.Sort).Select(z => new MenuTo
                        {
                            Id = z.Id,
                            Names = z.Names,
                            Url = z.Url,
                            Image = z.Image,
                            Remark = z.Remark
                        }).ToList()
                    }).ToList()
                }).ToList();
            }
            else
            {
                if (userRole != null)
                {
                    List<int> menuids = _context.TbMenuRoles.Where(k => k.RoleId == userRole.RoleId).Select(n => new { Id = n.MenuId }).ToList().Select(u => u.Id).ToList<int>();
                    tbMenus = tbMenus.Where(k => menuids.Contains(k.Id) && k.UserType == 0).ToList();
                    menus = tbMenus.Where(k => k.Pid == 0 && k.UserType == 0).OrderBy(k => k.Sort).Select(y => new Menu
                    {
                        Id = y.Id,
                        Names = y.Names,
                        Url = y.Url,
                        Image = y.Image,
                        Remark = y.Remark,
                        children = tbMenus.Where(k => k.Pid == y.Id && k.UserType == 0).OrderBy(k => k.Sort).Select(x => new MenuTo
                        {
                            Id = x.Id,
                            Names = x.Names,
                            Url = x.Url,
                            Image = x.Image,
                            Remark = x.Remark,
                            msgCount = GetUnauditedCount(x.Remark),
                            children = tbMenus.Where(k => k.Pid == x.Id && k.UserType == 0).OrderBy(k => k.Sort).Select(z => new MenuTo
                            {
                                Id = z.Id,
                                Names = z.Names,
                                Url = z.Url,
                                Image = z.Image,
                                Remark = z.Remark
                            }).ToList()
                        }).ToList()
                    }).ToList();
                }
            }
            ViewBag.menus = menus;
            if (menus.Count > 0)
            {
                var m = menus.Where(k => k.children.Count > 0).Select(k => k.children).FirstOrDefault();
                ViewBag.url = m.FirstOrDefault().Url;
            }
            else
            {
                ViewBag.url = "#";
            }
            ViewBag.webAfterLogo = _context.TbSystemConfigs.FirstOrDefault(k => k.MenuName == "webAfterLogo").Value;
            ViewBag.webAfterName = _context.TbSystemConfigs.FirstOrDefault(k => k.MenuName == "webAfterName" && k.Language == defaultLanguage).Value;
            ViewBag.webAfterTitle = _context.TbSystemConfigs.FirstOrDefault(k => k.MenuName == "webAfterTitle" && k.Language == defaultLanguage).Value;
            return View(model);
        }

        public int GetUnauditedCount(string auditorType)
        {
            int count = 0;
            switch (auditorType)
            {
                case "certificationAudit":
                    count = _context.CrmUserInfos.Count(k => k.State == 0);
                    break;
                case "depositReview":
                    count = _context.CrmDeposits.Count(k => k.State == 0);
                    break;
                case "cashVerification":
                    count = _context.CrmWithdrawals.Count(k => k.State == 0);
                    break;
                case "proxyAudit":
                    count = _context.Promotions.Count(k => k.State == 0);
                    break;
                case "internalTransfer":
                    count = _context.CrmInternaltransfers.Count(k => k.State == 0);
                    break;
            }
            return count;
        }

        public IActionResult Console()
        {
            return View();
        }

        /// <summary>
        /// 系统开发中
        /// </summary>
        /// <returns></returns>
        public IActionResult Dev()
        {
            return View();
        }


        public IActionResult License()
        {
            return View();
        }

        public IActionResult Organization()
        {
            return View();
        }

        public IActionResult Log()
        {
            return View();
        }
        [HttpGet]
        public IActionResult Logout(string language)
        {
            DeleteSessions("User");
            DeleteCookies("User");
            return Redirect($"~/Console/Login/Index?language={language}");
        }
    }
}